from django.db import models

# Create your models here.


class BooInfo(models.Model):
    btitle = models.CharField(max_length=20, verbose_name='名称')
    bpub_date = models.DateField(verbose_name='发布日期')
    bread = models.IntegerField(default=0, verbose_name='阅读量')
    bcomment = models.IntegerField(default=0, verbose_name='评论量')
    is_delete = models.BooleanField(default=False, verbose_name="逻辑删除")
    bk_count = models.IntegerField(default=0, null=True, verbose_name="人物数量")
    image = models.ImageField(upload_to='booktest', verbose_name='图片', null=True)

    class Meta:
        db_table = "tb_books"
        verbose_name = "图书"  # 别名,在admin站点中显示的名称
        verbose_name_plural = verbose_name  # 模型的复数形式,若不指定,Django会自动在模型名称后增加s(即"图书s")

    def __str__(self):
        """定义每个数据对象的显示信息"""
        return self.btitle

    def pub_date(self):
        return self.bpub_date.strftime("%Y-%m-%d")
        # 设置方法在admin中的显示效果

    pub_date.short_description = "发布日期"  # 设置列表中标题的名称
    pub_date.admin_order_field = "bpub_date"  # 设置排序属性


# 定义人物类  多
class HeroInfo(models.Model):
    GENDER_CHOICES = (
        (0, "male"),
        (1, "femal")
    )
    hname = models.CharField(max_length=20, verbose_name="名称")
    hgender = models.SmallIntegerField(choices=GENDER_CHOICES, default=0, verbose_name="性别")
    hcomment = models.CharField(max_length=200, null=True, verbose_name="描述信息")
    hbook = models.ForeignKey(BooInfo, on_delete=models.CASCADE, verbose_name="图书")  # 一对多
    is_delete = models.BooleanField(default=False, verbose_name="逻辑删除")

    class Meta:
        db_table = "tb_heros"
        verbose_name = "人物"
        verbose_name_plural = verbose_name

    def __str__(self):
        return self.hname
